package com.efounder.view;

import android.app.Activity;
import androidx.appcompat.app.AlertDialog;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.mobilemanager.R;

/**
 * Created by Administrator on 2018/1/24 0024.
 * <p>
 * 弹出升级信息的那个view
 */

public class NewVersionAlertDialog {

    private Activity mContext;
    private int mWidth;
    private int mHeight;
    /**
     * 升级的文字信息
     */
    private TextView newVersionInforTv;
    /**
     * 进度条
     */
    private FlikerProgressBar round_flikerbar;
    /**
     * 面板占屏幕宽度的百分比
     */
    private static final float WIDTHFRACTION = 0.8f;
    private static final float HEIGHTFRACTION = 1f;
    private AlertDialog alertDialog;
    private View newVersionContentView;
    private View cancleIv;
    private TextView newVersionTitleTv;

    /**
     * 用户是否可以中途打断下载
     */
    private boolean canPause = true;
    private View space;

    /**
     * 用户是否可以中途打断下载
     */
    public void setCanPause(boolean canPause) {
        this.canPause = canPause;
        if (currentStatus != 0) {
            round_flikerbar.setClickable(canPause);
        }
    }

    /**
     * 用户是否可以中途取消下载
     */
    @Deprecated
    public void setCanCancle(boolean canCancle) {
        this.canCancle = canCancle;
        cancleIv.setVisibility(canCancle ? View.VISIBLE : View.GONE);
        space.setVisibility(canCancle ? View.VISIBLE : View.GONE);
    }

    /**
     * 用户是否可以取消下载
     */
    private boolean canCancle = true;
    /**
     * 判断用户的操作状态
     * 0默认状态
     * 1正在下载状态
     * 2暂停下载状态
     */
    private int currentStatus = 0;

    private void getWindowWidthAndHeight() {
        WindowManager m = mContext.getWindowManager();
        Display d = m.getDefaultDisplay();
        mWidth = d.getWidth();
        mHeight = d.getHeight();
    }

    /**
     * 拿到返回键
     *
     * @return
     */
    public View getCancleIv() {
        return cancleIv;
    }

    public NewVersionAlertDialog(Activity context) {
        this.mContext = context;
        getWindowWidthAndHeight();
        init();
    }

    UploadListener mL;

    public void setListener(UploadListener ml) {
        this.mL = ml;
        if (mL == null) {
            return;
        }
        cancleIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mL != null) {
                    mL.onCancle();
                }
            }
        });
        round_flikerbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentStatus == 0) {
                    //用户第一次点击
                    if (mL != null) {
                        mL.onBegin();
                    }

                    round_flikerbar.setClickable(canPause);
                    currentStatus = 1;
                } else if (currentStatus == 1) {
                    //用户想要暂停
                    mL.onPause();
                    round_flikerbar.setStop(true);
                    currentStatus = 2;
                } else if (currentStatus == 2) {
                    //用户想要继续
                    round_flikerbar.setStop(false);
                    mL.onResume();
                    currentStatus = 1;
                }
            }
        });
    }

    /**
     * 设置升级的信息
     * 最多八行
     *
     * @param msg
     */
    public void setTextInfo(String msg) {
        newVersionInforTv.setText(msg);
    }

    /**
     * 设置标题
     *
     * @param msg
     */
    public void setTitle(String msg) {
        newVersionTitleTv.setText(msg);
    }

    /**
     * 拿到进度条
     *
     * @return
     */
    public FlikerProgressBar getProgressBar() {
        return round_flikerbar;
    }

    private void init() {
        newVersionContentView = LayoutInflater.from(mContext).inflate(R.layout.new_version_content, null);
        ImageView newVersionBg = (ImageView) newVersionContentView.findViewById(R.id.new_version_iv);
        newVersionInforTv = (TextView) newVersionContentView.findViewById(R.id.new_versio_infor_tv);
        space = newVersionContentView.findViewById(R.id.space);
        cancleIv = newVersionContentView.findViewById(R.id.cancleIv);
        cancleIv.setVisibility(canCancle ? View.VISIBLE : View.GONE);
        space.setVisibility(canCancle ? View.VISIBLE : View.GONE);

        newVersionTitleTv = (TextView) newVersionContentView.findViewById(R.id.new_version_title);

        round_flikerbar = (FlikerProgressBar) newVersionContentView.findViewById(R.id.round_flikerbar);

        //根据图片的宽高等比例缩小 84 /33 = WIDTHFRACTION* mWidth / x;
        int x = (int) (WIDTHFRACTION * mWidth * 33 / 84);
        newVersionBg.setLayoutParams(new FrameLayout.LayoutParams((int) (WIDTHFRACTION * mWidth), x));
        alertDialog = new AlertDialog.Builder(mContext)
                .setCancelable(false).create();
        alertDialog.setView(newVersionContentView);
    }


    public void show() {

        alertDialog.show();
        Window window = alertDialog.getWindow();
        window.setBackgroundDrawableResource(R.drawable.new_version_round_shape);
        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.new_version_scale_open);
        newVersionContentView.startAnimation(animation);
        setDialogSize(alertDialog, newVersionContentView);

    }

    public void dismiss() {
        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.new_version_scale_close);
        newVersionContentView.startAnimation(animation);
        alertDialog.dismiss();
        round_flikerbar.reset();
    }

    /**
     * 根据屏幕的宽度来设置面板的宽度
     * 高度自适应
     *
     * @param alertDialog
     * @param view
     */
    private void setDialogSize(AlertDialog alertDialog, View view) {
        //为获取屏幕宽、高
        WindowManager.LayoutParams p = alertDialog.getWindow().getAttributes();  //获取对话框当前的参数值
        p.height = WindowManager.LayoutParams.WRAP_CONTENT;
        p.width = (int) (mWidth * WIDTHFRACTION);
        p.gravity = Gravity.CENTER;
        alertDialog.getWindow().setAttributes(p);
    }

    public void setProgress(float progress) {
        round_flikerbar.setProgress(progress);
    }


    public interface UploadListener {
        /**
         * 当用户点击开始按钮
         */
        void onBegin();

        /***
         * 当用户点击暂停按钮
         */
        void onPause();

        /**
         * 当用户点击取消按钮
         */
        void onCancle();

        /**
         * 用户想要继续下载
         */
        void onResume();
    }
}
